﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Common.DataStructures;

namespace Boggle
{
    public class BoggleDictionary : IBoggleDictionary
    {
        private readonly Trie<String> _dictionary = new Trie<String>();

        /// <summary>
        /// Creates a dictionary using the BoggleDictionary.txt embedded resource
        /// </summary>
        public BoggleDictionary()
        {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            using (Stream dictionaryStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + ".BoggleDictionary.txt"))
            using (StreamReader dictionaryStreamReader = new StreamReader(dictionaryStream))
            {
                String entry;
                while ((entry = dictionaryStreamReader.ReadLine()) != null)
                {
                    _dictionary.AddValue(entry, entry);
                }
            }
        }

        public BoggleDictionary(IEnumerable<string> entries)
        {
            foreach (string entry in entries)
            {
                _dictionary.AddValue(entry, entry);
            }
        }

        public BoggleDictionary(string dictionaryPath)
        {
            foreach (string word in File.ReadAllLines(dictionaryPath))
            {
                _dictionary.AddValue(word, word);
            }
        }

        public SearchResult Search(string word)
        {
            return _dictionary.Search(word);
        }
    }


}
